package com.lihaichen.utils.http.poc.stackoverflow;

import org.apache.commons.codec.binary.Base64;
//import org.apache.commons.httpclient.HttpClient;
//import org.apache.commons.httpclient.HttpStatus;
//import org.apache.commons.httpclient.UsernamePasswordCredentials;
//import org.apache.commons.httpclient.methods.PostMethod;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpStatus;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

/*
have a web-application running in tomcat,
 based on certain actions in my web-applications
 I need to write a java code which would trigger curl (http://curl.haxx.se).
 Curl would then query a third-party application and return an XML/JSON.

This has to be read by me and return appropriate response to the user.
I know this can be done with curl
 but using a command line tool for making request from a web application
 is not the best way to go about it
 and thus I have written a code in Java, httpclient API.

The curl code was

curl
 -u username:password
 -d "param1=aaa& param2=bbb"
 -k http://www.testme.com/api/searches.xml

curl by default uses base64 encoding.
 Thus the corresponding code in Java which is written by me is
 */

/**
 *
 * @deprecated
 * 
 * */
public class CurlSample {

    public static void main(String args[]) {

//        String username = "abc";
//        String password = "xyz";
//
//        HttpClient httpclient = new DefaultHttpClient();
//        BufferedReader bufferedreader = null;
//
//
//        PostMethod postmethod = new PostMethod("https://www.testabc.com/api/searches.xml");
//        postmethod.addParameter("_def_id", "8");
//        postmethod.addParameter("dobday", "6");
//        postmethod.addParameter("dobmonth", "6");
//        postmethod.addParameter("dobyear", "1960");
//        postmethod.addParameter("firstname", "Test");
//        postmethod.addParameter("lastname", "Test");
//
//
//        String username_encoded = new String(Base64.encodeBase64(username.getBytes()));
//        System.out.println("username_encoded =" + username_encoded);
//
//        String password_encoded = new String(Base64.encodeBase64(password.getBytes()));
//        System.out.println("password_encoded =" + password_encoded);
//
//        httpclient.getState().setAuthenticationPreemptive(true);
//        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials();
//        credentials.setPassword(username_encoded);
//        credentials.setUserName(password_encoded);
//
//        httpclient.getState().setCredentials("FORM", "http://www.testabc.com/api/searches.xml", credentials);   // I am not sure which one to use here..
//
//        try {
//            int rCode = httpclient.executeMethod(postmethod);
//            System.out.println("rCode is" + rCode);
//
//            if (rCode == HttpStatus.SC_NOT_IMPLEMENTED) {
//                System.err.println("The Post postmethod is not implemented by this URI");
//                postmethod.getResponseBodyAsString();
//            } else if (rCode == HttpStatus.SC_NOT_ACCEPTABLE) {
//                System.out.println(postmethod.getResponseBodyAsString());
//            } else {
//                bufferedreader = new BufferedReader(new InputStreamReader(postmethod.getResponseBodyAsStream()));
//                String readLine;
//
//                while (((readLine = bufferedreader.readLine()) != null)) {
//                    System.out.println("return value " + readLine);
//                }
//
//            }
//        } catch (Exception e) {
//            System.err.println(e);
//        } finally {
//            postmethod.releaseConnection();
//            if (bufferedreader != null) {
//                try {
//                    bufferedreader.close();
//                } catch (Exception fe) {
//                    fe.printStackTrace();
//                }
//            }
//        }
}
}

